package com.etime.shuhuiyuan.services.courseadmin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.CourseAdminDao;
import com.etime.shuhuiyuan.dao.CourseDao;
import com.etime.shuhuiyuan.dto.CourseAdminDto;
import com.etime.shuhuiyuan.pojo.Course;
import com.etime.shuhuiyuan.services.courseadmin.interfaces.CourseAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class CourseAdminServiceImpl implements CourseAdminService {

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private CourseAdminDao courseAdminDao;

    @Override
    public List<Course> selectCourseList(Course course) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if(course!=null && course.getCoursetitle()!=null && !course.getCoursetitle().equalsIgnoreCase("")) {
            wrapper.eq("coursetitle", course.getCoursetitle());
        }
        return courseDao.selectList(wrapper);
    }

    @Override
    public Page<Course> selectCourseListByPage(Course course, long pageNum, long pageSize) {
        Page<Course> page = new Page<>(pageNum,pageSize);

        QueryWrapper<Course> wrapper = new QueryWrapper<>();

        if(course!=null && course.getCoursetitle()!=null && !course.getCoursetitle().equalsIgnoreCase("")) {
            wrapper.eq("coursetitle", course.getCoursetitle());
        }
        return courseDao.selectPage(page,wrapper);
    }

    @Override
    public Page<CourseAdminDto> selectCourseAdminDtoByPage(CourseAdminDto courseAdminDto, long pageNum, long pageSize) {
        Page<CourseAdminDto> page = new Page<>(pageNum,pageSize);
        String coursetitle = "";
        Integer coursestate = null;
        Integer courseaudit = null;
        if (courseAdminDto!=null && courseAdminDto.getCoursetitle()!=null && !courseAdminDto.getCoursetitle().equalsIgnoreCase("")){
            coursetitle = courseAdminDto.getCoursetitle();
            coursetitle = "%"+coursetitle+"%";
        }
        if (courseAdminDto!=null && courseAdminDto.getCoursestate()!=-1){
            coursestate = courseAdminDto.getCoursestate();
        }
        if(courseAdminDto!=null && courseAdminDto.getCourseaudit()!=-1){
            courseaudit = courseAdminDto.getCourseaudit();
        }
        return courseAdminDao.selectCourseByPage(page,courseAdminDto,coursetitle,coursestate,courseaudit);
    }

    @Override
    public Course selectCourseById(int courseId) {
        return courseDao.selectById(courseId);
    }

    public CourseAdminDto selectCourseAdminDtoById(int courseId){
        return courseAdminDao.selectCourseAdminDtoById(courseId);
    }

    @Override
    public int updateCourseAdmin(Course course) {
        //设置修改时间
        long ts = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String time = simpleDateFormat.format(ts);
        //把格式化后的时间存入courseupdatetime
        course.setCourseupdatetime(time);

        return courseDao.updateById(course);
    }
}
